package ngram_reader.viewer.settings;

import java.util.prefs.Preferences;
import main.Main;
import swingExtended.preferences.PreferenceGroup;

/**
 *
 * @author Shimu
 */
public class SettingsDialog extends javax.swing.JDialog {

    /** Creates new form SettingsDialog */
    public SettingsDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.setLocationRelativeTo(parent);

        // Get the preferences
        final PreferenceGroup PREF_GROUP = new PreferenceGroup(Preferences.userRoot().node(
                Main.USER_PREF_ROOT_NODE + "/" + this.getClass()));

        PREF_GROUP.add(checkBoxStopOnError, "stopOnError");
        PREF_GROUP.add(checkBoxClearOutputOnStart, "clearOutputOnStart");

        checkBoxStopOnError.setVisible(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tabbedPane = new javax.swing.JTabbedPane();
        nGramLocationPanel = new ngram_reader.viewer.settings.NGramLocationPanel();
        panelOutputFormat = new javax.swing.JPanel();
        checkBoxClearOutputOnStart = new javax.swing.JCheckBox();
        checkBoxStopOnError = new javax.swing.JCheckBox();
        buttonSave = new javax.swing.JButton();

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });

        tabbedPane.addTab("NGram Location", nGramLocationPanel);

        checkBoxClearOutputOnStart.setSelected(true);
        checkBoxClearOutputOnStart.setText("Clear output boxes on start");
        checkBoxClearOutputOnStart.setToolTipText("Clear all the output boxes when \"Start\" is pressed");

        checkBoxStopOnError.setText("Stop on first error");
        checkBoxStopOnError.setToolTipText("Abort the operation if an error is encountered.");

        javax.swing.GroupLayout panelOutputFormatLayout = new javax.swing.GroupLayout(panelOutputFormat);
        panelOutputFormat.setLayout(panelOutputFormatLayout);
        panelOutputFormatLayout.setHorizontalGroup(
            panelOutputFormatLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelOutputFormatLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelOutputFormatLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(checkBoxClearOutputOnStart)
                    .addComponent(checkBoxStopOnError))
                .addContainerGap(245, Short.MAX_VALUE))
        );
        panelOutputFormatLayout.setVerticalGroup(
            panelOutputFormatLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelOutputFormatLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(checkBoxClearOutputOnStart)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(checkBoxStopOnError)
                .addContainerGap(181, Short.MAX_VALUE))
        );

        checkBoxClearOutputOnStart.getAccessibleContext().setAccessibleName("checkBoxClearOnStart");

        tabbedPane.addTab("Output Format", panelOutputFormat);

        buttonSave.setText("Save");
        buttonSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonSaveActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(tabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 479, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(399, 399, 399)
                .addComponent(buttonSave, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(tabbedPane, javax.swing.GroupLayout.PREFERRED_SIZE, 266, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(buttonSave)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        getRootPane().setDefaultButton(buttonSave);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public NGramLocationPanel getNGramLocationPanel() {
        return nGramLocationPanel;
    }

    public boolean clearOutputOnStart() {
        return checkBoxClearOutputOnStart.isSelected();
    }

    public boolean stopOnError() {
        return checkBoxStopOnError.isSelected();
    }

    /** Closes the dialog */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        doClose();
    }//GEN-LAST:event_closeDialog

private void buttonSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonSaveActionPerformed
    doClose();
}//GEN-LAST:event_buttonSaveActionPerformed

    private void doClose() {
        setVisible(false);
        dispose();
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonSave;
    private javax.swing.JCheckBox checkBoxClearOutputOnStart;
    private javax.swing.JCheckBox checkBoxStopOnError;
    private ngram_reader.viewer.settings.NGramLocationPanel nGramLocationPanel;
    private javax.swing.JPanel panelOutputFormat;
    private javax.swing.JTabbedPane tabbedPane;
    // End of variables declaration//GEN-END:variables
}
